/* 
 * @description: 输入框组件封装
 * @author: Jane 
 * @date: 2023-09-12 17:29:41
 */
import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_widgets/text_widget.dart';
import 'package:flutter/services.dart';
import 'image_widget.dart';
import 'input_format.dart';

/// 登录 输入框
Widget inputBox(
    {required TextEditingController inputController,
    String hintText = "请输入手机号",
    String regExp = '',
    bool obscureText = false,
    void Function(String)? onChanged}) {
  return SizedBox(
    child: TextField(
      cursorColor: AppTheme.dartTextColor,
      onChanged: onChanged,
      obscureText: obscureText,
      controller: inputController,
      style: const TextStyle(color: AppTheme.dartTextColor),
      inputFormatters: regExp.isEmpty ? [] : [InputFormatter(regExp)],
      decoration: InputDecoration(
        contentPadding: EdgeInsets.symmetric(
            vertical: 4.0, horizontal: ScreenHelper.width(10)),
        hintText: hintText,
        enabledBorder: InputBorder.none,
        focusedBorder: InputBorder.none,
        filled: true,
        fillColor: AppTheme.appBgColor,
        hintStyle: TextStyle(fontSize: ScreenHelper.sp(13)),
      ),
      keyboardType: TextInputType.phone,
      textInputAction: TextInputAction.done,
    ),
  );
}

/// 搜索框
///  onSubmitted 方法
Widget searchBox(
    {required TextEditingController searchController,
    void Function(String)? onSubmitted,
    bool isEnable = false}) {
  return Container(
    padding: const EdgeInsets.only(right: 10.0),
    color: Colors.white,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Expanded(
            flex: 2,
            child: Container(
              alignment: Alignment.centerLeft,
              height: ScreenHelper.height(45),
              margin: const EdgeInsets.symmetric(horizontal: 10),
              padding: EdgeInsets.all(ScreenHelper.width(4)),
              child: TextField(
                cursorColor: AppTheme.hintTextColor,
                controller: searchController,
                keyboardType: TextInputType.text,
                textInputAction: TextInputAction.search,
                autofocus: false,
                minLines: 1,
                maxLines: 1,
                style: TextStyle(color: AppTheme.dartTextColor,fontSize: ScreenHelper.sp(14)),
                textAlign: TextAlign.left,
                decoration: InputDecoration(
                    enabled: isEnable,
                    hintText: "请输入搜索内容",
                    hintStyle: TextStyle(fontSize: ScreenHelper.sp(14)),
                    contentPadding:
                        const EdgeInsets.symmetric(vertical: 4, horizontal: 10),
                    filled: true,
                    fillColor: AppTheme.appBgColor,
                    focusedBorder: const OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(50)),
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    disabledBorder: const OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(50)),
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    enabledBorder: const OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(50)),
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    border: const OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(50)),
                        borderSide:
                            BorderSide(width: 0, color: Colors.transparent)),
                    prefixIcon: Icon(
                      Icons.search,
                      size: ScreenHelper.width(22),
                      color: AppTheme.dartTextColor,
                    )),
                onSubmitted: isEnable ? onSubmitted : null,
              ),
            )),
        Offstage(
          offstage: !isEnable,
          child: GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                "取消",
                style: TextStyle(
                    fontSize: ScreenHelper.sp(16),
                    color: AppTheme.weakTextColor,
                    fontWeight: FontWeight.w500),
              ),
            ),
          ),
        )
      ],
    ),
  );
}

/// 信息输入
/// attrName 属性名称
/// hintStr 提示文本内容
/// isRequired 是否必填项
/// arrowChild 右侧组件
Widget buildInputInfoItem(
    {required attrName,
    required Widget arrowChild,
    bool isRequired = true,
    double padding = 15,
    Color bgColor=AppTheme.appBgColor,
    Color attrColor=AppTheme.dartTextColor,
    void Function()? onTap}) {
  return GestureDetector(
    onTap: onTap,
    child: Container(
      width: ScreenHelper.screenWidth,
      alignment: Alignment.center,
      color: bgColor,
      padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(padding)),
      height: ScreenHelper.height(50),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Offstage(
                offstage: !isRequired,
                child: Text(
                  "*",
                  style: TextStyle(
                      color: AppTheme.redtextColor,
                      fontSize: ScreenHelper.sp(16)),
                ),
              ),
              Text(
                attrName,
                style: TextStyle(
                    color: attrColor,
                    fontSize: ScreenHelper.sp(16)),
              )
            ],
          ),
          Expanded(flex: 2, child: arrowChild)
        ],
      ),
    ),
  );
}

/// 多行输入框
Widget inputAreaBox(
    {required String title,
    required String hint,
    required TextEditingController textControlller,
    String countNum = '0/500',
    int minLines = 4}) {
  return Container(
    color: Colors.white,
    alignment: Alignment.topLeft,
    padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
    child: Column(
      children: [
        norTitle(title: title),
        mutileTextField(
            hintText: hint,
            textController: textControlller,
            countNum: countNum,
            minLines: minLines)
      ],
    ),
  );
}

/// 多行 输入框
Widget mutileTextField(
    {required String hintText,
    required TextEditingController textController,
    String countNum = '0/500',
    int minLines = 4}) {
  return Container(
    padding: EdgeInsets.only(bottom: ScreenHelper.width(10)),
    decoration: BoxDecoration(
        border: Border.all(width: 1, color: AppTheme.dividerColor),
        borderRadius: BorderRadius.circular(10),
        color: Colors.white),
    child: TextField(
      controller: textController,
      keyboardType: TextInputType.multiline,
      minLines: minLines,
      maxLines: 10,
      cursorColor: AppTheme.weakTextColor,
      decoration: InputDecoration(
          border: InputBorder.none,
          hintText: hintText,
          hintStyle: TextStyle(
              color: AppTheme.hintTextColor, fontSize: ScreenHelper.sp(14)),
          contentPadding: EdgeInsets.all(ScreenHelper.width(10)),
          counterText: "${countNum}",
          counterStyle: TextStyle(color: AppTheme.hintTextColor)),
      textInputAction: TextInputAction.send,
        style: const TextStyle(
                                    color: AppTheme.dartTextColor),
    ),
  );
}

/// 无边框数据框
Widget inputBoxNoBorder(
    {required TextEditingController inputController,
    required String hint,
    FocusNode? focusNode,
    bool isEnable = true,
    EdgeInsetsGeometry? contentPadding,
    TextAlign textAlign = TextAlign.right,
    List<TextInputFormatter>? inputFormatters,
    void Function(String)? onSubmitted,
    void Function()? onEditingComplete,
    TextInputAction? textInputAction}) {
  return Container(
    margin: const EdgeInsets.only(left: 10),
    alignment: Alignment.centerRight,
    // height: 60,
    child: TextField(
        controller: inputController,
        cursorColor: AppTheme.weakTextColor,
        textAlign: textAlign,
        focusNode: focusNode,
        style: TextStyle(color: isEnable?AppTheme.dartTextColor: AppTheme.weakTextColor),
        textInputAction: textInputAction,
        onSubmitted: onSubmitted,
        
         inputFormatters:inputFormatters,
        onEditingComplete: onEditingComplete,
        decoration: InputDecoration(
            hintText: hint,
            enabled: isEnable,
            hintStyle: TextStyle(
                color: AppTheme.hintTextColor, fontSize: ScreenHelper.sp(16)),
            contentPadding: contentPadding??const EdgeInsets.only(top: 4, left: 10),
            border: InputBorder.none)),
  );
}


/// 修改资料
Widget buildUpdateInfoItem(
    {required String attrName,
    required String value,
    bool isHead = false,
    bool isEnalbe = true,
    FocusNode? focusNode,
    void Function()? onEditingComplete,
    void Function(String)? onSubmitted,
    required TextEditingController inputController}) {
  return Container(
    width: ScreenHelper.screenWidth,
    height: ScreenHelper.height(50),
    decoration: const BoxDecoration(
      color: Colors.white,
    ),
    padding: EdgeInsets.symmetric(
        horizontal: ScreenHelper.width(10), vertical: ScreenHelper.height(2)),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          width: ScreenHelper.width(90),
          child: Text(
            attrName,
            style: TextStyle(
                color: AppTheme.seconddarkWeakTextColor,
                fontSize: ScreenHelper.sp(16)),
          ),
        ),
        SizedBox(
          width: ScreenHelper.width(20),
        ),
        isHead
            ? circleImg(head: value, raduis: 20)
            : Expanded(
                child: Container(
                  alignment: Alignment.centerLeft,
                  child: inputBoxNoBorder(
                      hint: attrName,
                      inputController: inputController,
                      focusNode: focusNode,
                      textInputAction: TextInputAction.go,
                      onSubmitted: onSubmitted,
                      onEditingComplete: onEditingComplete,
                      textAlign: TextAlign.left,
                      isEnable: isEnalbe),
                ),
              ),
        Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: Image.asset(
              "assets/profile/icon_arrow_next.png",
              width: ScreenHelper.width(24),
              package: "basic_common",
            ),
          ),
        ),
      ],
    ),
  );
}


/// 基本信息
Widget buildBaseInfoItem(
    {required String attrName, required String value,double width=90,bool isHead = false, bool isArrow = false,bool isEnable=true}) {
  return Container(
    width: ScreenHelper.screenWidth,
    height: ScreenHelper.height(50),
    decoration: BoxDecoration(
      color: Colors.white,
    ),
    padding: EdgeInsets.symmetric(
        horizontal: ScreenHelper.width(10), vertical: ScreenHelper.height(2)),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          width: ScreenHelper.width(width),
          child: Text(
            attrName,
            style: TextStyle(
                color: AppTheme.weakTextColor, fontSize: ScreenHelper.sp(16)),
          ),
        ),
        SizedBox(
          width: ScreenHelper.width(20),
        ),
        isHead
            ? circleImg(head: value, raduis: 20)
            : Expanded(
                child: Text(
                  value,
                  softWrap: true,
                  maxLines: 2,
                  style: TextStyle(
                      color: isEnable?AppTheme.dartTextColor:AppTheme.weakTextColor,
                      fontSize: ScreenHelper.sp(14)),
                ),
              ),
        isArrow?Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: Image.asset(
              "assets/profile/icon_arrow_next.png",
              width: ScreenHelper.width(24),
              package: "basic_common",
            ),
          ),
        ):SizedBox.shrink(),
      ],
    ),
  );
}


